package com.example.account.controller;

import com.example.account.domain.Account;
import com.example.account.domain.Info;
import com.example.account.domain.User;
import com.example.account.repository.AccountRepository;
import com.example.account.repository.InfoRepository;
import com.example.account.repository.UserRepository;
import com.example.account.service.AccountService;
import com.example.account.service.InfoService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class InfoController {
    @Autowired
    private InfoRepository infoRepository;
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private InfoService infoService;

    @RequestMapping(value = "/getinfo", method = RequestMethod.GET)
    public Object getBills(@RequestParam("tel") String tel
    ) {
        List<Map> list = new ArrayList<>();

        for (Info i : infoRepository.findAll()) {
            Map<Object, Object> map = new HashMap<>();
            if (tel.equals(i.getTel())) {
                map.put("fid", i.getFid());
                map.put("age", i.getAge());
                map.put("height", i.getHeight());
                map.put("weight", i.getWeight());
                map.put("blood", i.getBlood());


            }
        }
        return list;
    }

    @PostMapping(value = "/info")
    public String info(
                            @RequestParam("tel") String tel,
                            @RequestParam("age") Integer age,
                            @RequestParam("height") Integer height,
                            @RequestParam("weight") Integer weight,
                            @RequestParam("blood") String blood

    ) throws Exception {

        Info info = new Info();

        info.setTel(tel);
        info.setAge(age);
        info.setHeight(height);
        info.setWeight(weight);
        info.setBlood(blood);
        infoRepository.save(info);

        return "信息补充成功！";
    }
    @PostMapping(value = "/infoalter")
    public String infoalter(
            @RequestParam("fid") Long fid,
            @RequestParam("tel") String tel,
            @RequestParam("age") Integer age,
            @RequestParam("height") Integer height,
            @RequestParam("weight") Integer weight,
            @RequestParam("blood") String blood

    ) throws Exception {

        Info info = new Info();
        info.setFid(fid);
        info.setTel(tel);
        info.setAge(age);
        info.setHeight(height);
        info.setWeight(weight);
        info.setBlood(blood);
        infoRepository.save(info);

        return "信息修改成功！";
    }

}
